package com.example.demo.web;

import com.example.demo.module.UserBean;
import com.example.demo.oauth.OauthWeixin;
import com.example.demo.service.UserService;
import com.example.demo.service.UserThirdService;
import com.example.demo.util.GenerateToken;
import com.example.demo.util.TokenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:lgl
 * @Description:
 * @Date:Create in 2017/6/14 .
 */
@Controller
@CrossOrigin
public class LoginController {

    protected static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private UserThirdService userThirdService;

    @GetMapping("login")
    public String login() {
        UserBean user = userService.findById("1");
        System.out.println(user);
        return "i-login";
    }

    @GetMapping("user/login")
    @ResponseBody
    public Map<String, String> userLogin(HttpServletRequest request) {
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        System.out.println(password+phone);
            UserBean user = userService.login(phone, password);
            if(ObjectUtils.isEmpty(user)){
                user = userService.selectByUserBind(phone,password);
            }
            Map<String,String> map = new HashMap<>();
            if (!ObjectUtils.isEmpty(user)) {
                String token = GenerateToken.newToken(user.getId());
                user.setToken(token);
                request.getSession().setAttribute("user", user);
                map.put("obj","success");
                return map;
            }else {
                map.put("obj","error");
                return map;
            }
       // return getToken(user,request);
    }


    @PostMapping("login/weixin")
    @ResponseBody
    public Map<String,String> view2() {
            OauthWeixin ow=OauthWeixin.me();
            String url = ow.getAuthorizeUrl(TokenUtil.randomState());
             Map map = new HashMap();
             map.put("oauthUrl",url);
        System.out.println("url"+url);
             return map;
    }


    @GetMapping("weixin/callback")
    public ModelAndView weiXinCallback(HttpServletRequest request) {
        String code = request.getParameter("code");
        logger.debug(code);
        System.out.println(code);
        UserBean user = userThirdService.weiXinCallback(code);
        String token = GenerateToken.newToken(user.getId());
        user.setToken(token);
        request.getSession().setAttribute("user", user);
        System.out.println(user.toString());
        return new ModelAndView("redirect:/homePage");
    }

    @GetMapping("/homePage")
    public String login(ModelMap map, HttpServletRequest request) {
        UserBean user = (UserBean) request.getSession().getAttribute("user");
        if(ObjectUtils.isEmpty(user)){
            return "error";
        }
        map.put("user",user);
        return "i-index";
    }


    @GetMapping("app/login")
    public String appLogin(String userId,HttpServletRequest request) {
        UserBean user = userService.findById(userId);
        if(!ObjectUtils.isEmpty(user)) {
            String token = GenerateToken.newToken(user.getId());
            user.setToken(token);
            request.getSession().setAttribute("user", user);
            return "redirect:/homePage";
        }
        return "error";
    }

}
